//
//  DetailTravelTableViewCell.m
//  EDPS
//
//  Created by 张维扬 on 15/11/27.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "DetailTravelTableViewCell.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
@interface DetailTravelTableViewCell ()
@property (nonatomic, retain) UIImageView *photo;// 图片
@property (nonatomic, retain) UIView *bg;//背景颜色的View
@property (nonatomic, retain) UIImageView *corner;//三角
@property (nonatomic, retain) UILabel *text; // 下面文本
@property (nonatomic, retain) UILabel *erectLabel;// 竖线label
@property (nonatomic, retain) UILabel *grayLineLabel; // 文本下面的灰线
@property (nonatomic, retain) UIImageView *timeImg;// 时间图片
@property (nonatomic, retain) UILabel *timeLabel;// 时间label

@end

@implementation DetailTravelTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.bg = [[UIView alloc] init];
        self.bg.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.bg];
        
        self.photo = [[UIImageView alloc] init];
        [self.bg addSubview:self.photo];
        
        self.corner = [[UIImageView alloc] init];
        self.corner.image = [UIImage imageNamed:@"travel_detail"];
        [self.photo addSubview:self.corner];
        
        self.text = [[UILabel alloc] init];
        [self.text setFont:[UIFont systemFontOfSize:15]];
        self.text.textColor = [UIColor darkGrayColor];
        self.text.numberOfLines = 0;
//        self.text.backgroundColor = [UIColor grayColor];
        [self.bg addSubview:self.text];
        
        self.grayLineLabel = [[UILabel alloc] init];
        self.grayLineLabel.backgroundColor = [UIColor lightGrayColor];
        [self.bg addSubview:self.grayLineLabel];
        
        self.erectLabel = [[UILabel alloc] init];
        self.erectLabel.backgroundColor = RGB(224, 215, 183);
        [self.contentView addSubview:self.erectLabel];
        
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.textColor = [UIColor darkGrayColor];
        [self.timeLabel setFont:[UIFont systemFontOfSize:13]];
        [self.bg addSubview:self.timeLabel];
        
        self.timeImg = [[UIImageView alloc] init];
        self.timeImg.image = [UIImage imageNamed:@"iconfont-time"];
        [self.bg addSubview:self.timeImg];
        
        self.map_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.bg addSubview:self.map_Btn];
        self.map_Btn.titleLabel.font = [UIFont systemFontOfSize:12];
        self.map_Btn.layer.cornerRadius = 10;
        [self.map_Btn setTitleColor:RGBA(62, 172, 190, 1) forState:UIControlStateNormal];
//        self.map_Btn.backgroundColor = [UIColor grayColor];
        [self.map_Btn addTarget:self action:@selector(map_BtnAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(0);
        make.left.mas_equalTo(self.mas_left).offset(20);
        make.right.mas_equalTo(self.mas_right).offset(-20);
        make.bottom.mas_equalTo(self.mas_bottom).offset(0);
    }];
    
    
    // 计算出文本高度
    CGFloat textHeight = [[self class] heightWithText:self.model.text fontSize:15 width: ScreenWidth - 40];
    
    CGFloat imgH = (self.model.h).integerValue;
    CGFloat imgW = (self.model.w).integerValue;
    
    if (imgH != 0 && imgW != 0) {
        // 图片
        [self.photo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.bg.mas_top).offset(0);
            make.left.mas_equalTo(self.bg.mas_left).offset(0);
            make.right.mas_equalTo(self.bg.mas_right).offset(0);
            // 长 X 宽 = 面积 -->  面积 / 宽 = 长
            make.size.mas_equalTo(CGSizeMake(ScreenWidth - 40, imgH *           (ScreenWidth -40) / imgW));
        }];
        // 文本高度
        [self.text mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.photo.mas_bottom).offset(5);
            make.right.mas_equalTo(self.bg.mas_right).offset(-10);
            make.left.mas_equalTo(self.photo.mas_left).offset(10);
            make.size.mas_equalTo(CGSizeMake(ScreenWidth - 40, textHeight));
            make.bottom.mas_equalTo(self.bg.mas_bottom).offset(-35);
        }];
        // 时间小图片
        [self.timeImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.bg.mas_left).mas_equalTo(10);
            make.bottom.mas_equalTo(self.bg.mas_bottom).mas_equalTo(-10);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
        // 时间label
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.timeImg.mas_centerY);
            make.left.mas_equalTo(self.timeImg.mas_right).offset(5);
            make.size.mas_equalTo(CGSizeMake(ScreenWidth - 70, 20));
        }];
        // 三角图片
        [self.corner mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.bg.mas_left).offset(0);
            make.bottom.mas_equalTo(self.photo.mas_bottom).offset(0);
            make.size.mas_equalTo(CGSizeMake(ScreenWidth - 40, 10));
        }];
        // 下面灰线
        [self.grayLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.bg.mas_bottom).offset(0);
            make.left.mas_equalTo(self.photo.mas_left).offset(0);
            make.size.mas_equalTo(CGSizeMake(ScreenWidth - 40, 3));
        }];
        
        // 下面竖线
        [self.erectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.bg.mas_bottom).offset(0);
            make.left.mas_equalTo(self.mas_left).offset(ScreenWidth * 0.04 + 27);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0);
            make.width.mas_equalTo(5);
        }];
        
        // 白色背景view
        [self.bg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top).offset(0);
            make.left.mas_equalTo(self.contentView.mas_left).offset(0);
            make.right.mas_equalTo(self.contentView.mas_right).offset(0);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-20);
        }];
        // 跳转btn
        [self.map_Btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.timeImg.mas_centerY);
            make.right.mas_equalTo(self.bg.mas_right).offset(-5);
            make.size.mas_equalTo(CGSizeMake(130, 25));
        }];
    }else{
        
    }
    
}

// 让代理人调用方法
- (void)map_BtnAction
{
    [self.mapDelegate pushToMapDelegate:self.indexPath];
}
- (void)setModel:(DetailTravelModel *)model
{
    if (_model != model) {
        _model = model;
    }
    
    [self.photo sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"hold"]];
    self.text.text = model.text;
    [self.map_Btn setTitle:[NSString stringWithFormat:@"%@,%@,%@", model.country, model.province, model.city] forState:UIControlStateNormal];
    self.timeLabel.text = model.local_time;
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
